/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package enterprise.web_jpa_war.servlet;

import enterprise.web_jpa_war.entity.Company;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import javax.persistence.PersistenceUnit;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityManager;
import javax.annotation.Resource;
import javax.transaction.UserTransaction;

/**
 *
 * @author GOSTEP
 */
@WebServlet(name="CreateCompanyServlet", urlPatterns={"/CreateCompany.do"})
public class CreateCompanyServlet extends HttpServlet {
     @PersistenceUnit
    //The emf corresponding to
    private EntityManagerFactory emf;

    @Resource
    private UserTransaction utx;

  
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
           
        } finally { 
            out.close();
        }
    } 

 
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
             assert emf != null;  //Make sure injection went through correctly.
        EntityManager em = null;
        try {

            //Get the data from user's form
            String companyname = request.getParameter("companyname");
            String comaddress  = request.getParameter("comaddress");
            String webaddress   = request.getParameter("webaddress");

            //Create a person instance out of it
            Company addresses = new Company(companyname, comaddress, webaddress);

            //begin a transaction
                   //begin a transaction
            utx.begin();
            //create an em.
            //Since the em is created inside a transaction, it is associsated with
            //the transaction
            em = emf.createEntityManager();
            //persist the person entity
            em.persist(addresses);
            //commit transaction which will trigger the em to
            //commit newly created entity into database
            utx.commit();

            
          request.setAttribute("message","You have created a new Company....");
          request.getRequestDispatcher("message.jsp").forward(request, response);
          
        }  catch (Exception ex) {
            throw new ServletException(ex);
        } finally {
            //close the em to release any resources held up by the persistebce provider
            if(em != null) {
                em.close();
            }
        }
  }


    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
